#include <vector>

using namespace std;

class Solution {
private:
    int age;
    vector<int> age1;
    bool dfs(vector<int>& postorder, int left, int right) {
        if (left >= right) {
            return true;
        }
        int p = left;
        while (postorder[p] < postorder[right]) {
            p++;
        }
        int r1 = p - 1;
        while (postorder[p] > postorder[right]) {
            p++;
        }
        return p == right && dfs(postorder, left, r1) && dfs(postorder, r1 + 1, right - 1);

    }
public:
    bool verifyPostorder(vector<int>& postorder) {
        return dfs(postorder, 0, postorder.size() - 1);
    }
};
